﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_ProDotuoi_Edit
    Inherits System.Web.UI.UserControl
#Region "Sub and Function "
    Dim level As String = -1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Dim data As New VSEntities
        Dim p As ProDoTuoi = (From q In data.ProDoTuois Where q.Id = hidID.Value).SingleOrDefault
        If Not p Is Nothing Then
            txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
            txtDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
        End If
    End Sub
#End Region
#Region "Event for control"
    Protected Function CheckEdit(ByVal strTitle As String, ByVal intID As Integer) As Boolean
        Using data As New VSEntities
            Dim check As ProDoTuoi = (From x In data.ProDoTuois Where Not (x.Id = intID) And x.Title.Equals(strTitle) Select x).SingleOrDefault
            If Not check Is Nothing Then
                Return True
            Else
                Return False
            End If
        End Using

    End Function
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If CheckEdit(txtTitle.Text.Trim, hidID.Value) Then
            Excute_Javascript("Alertbox('Độ tuổi đã tồn tại');", Me.Page, True)
        Else
            Using data As New VSEntities
                Dim p As ProDoTuoi = (From q In data.ProDoTuois Where q.Id = hidID.Value).SingleOrDefault
                Try
                    p.Id = hidID.Value
                    p.Title = txtTitle.Text.Trim()
                    p.Description = txtDescription.Text
                    data.SaveChanges()
                    'Insert_App_Log("Update Category:" & txtTitle.Text.Trim & "", Function_Name.Category, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/ProDoTuoi/List.aspx?Id=" & p.Id & "';", Me.Page, True)

                Catch ex As Exception
                    'log4net.Config.XmlConfigurator.Configure()
                    'log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End Using
        End If




    End Sub
    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
